package com.czwj.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.entities.PageBean;
import com.czwj.entities.PlanMain;
import com.czwj.entities.UserInfo;
import com.czwj.entities.BudgetMain;
import com.czwj.entities.DataGridData;
import com.czwj.service.BudgetItemService;
import com.czwj.service.BudgetMainService;
import com.czwj.service.JobberService;
import com.czwj.util.TextUtil;

import net.sf.json.JSONObject;

/**
* @ClassName: JobberAction
* @Description: TODO
* @author H
* @date 2020年4月25日
*
*/
@Controller
public class JobberAction{
	@Resource
	JobberService jobberService;
	@Resource
	BudgetItemService budgetItemService;
	@Resource
	BudgetMainService budgetMainService;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@RequestMapping(value = "/jobbers.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<PlanMain> planMain(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "jobno", required = false) String jobno,
			@RequestParam(value = "projectitemchildid", required = false) String projectitemchildid,
			@RequestParam(value = "projectitemid", required = false) String projectitemid) {
		DataGridData<PlanMain> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(name)){
			map.put("name", name);
		}
		if(TextUtil.isNotEmpty(jobno)){
			map.put("jobno", jobno);
		}
		if(TextUtil.isNotEmpty(projectitemid)&&!projectitemid.equals("0")){
			map.put("projectitemid", projectitemid);
		}
		if(TextUtil.isNotEmpty(projectitemchildid)&&!projectitemchildid.equals("0")){
			map.put("projectitemchildid", projectitemchildid);
		}
		List<PlanMain> list = jobberService.search(map);
		result.setRows(list);
		result.setTotal(jobberService.count(map));
		return result;
	}
	
	@RequestMapping(value = "/jobbersuser.do", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<PlanMain> jobbersuser(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "jobno", required = false) String jobno,
			@RequestParam(value = "projectitemchildid", required = false) String projectitemchildid,
			@RequestParam(value = "projectitemid", required = false) String projectitemid) {
		DataGridData<PlanMain> result = new DataGridData();
		Map<String, Object> map = new HashMap<String, Object>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)) {
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		}
		if (pageBean != null) {
			map.put("start", pageBean.getStart());
			map.put("size", pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(name)){
			map.put("name", name);
		}
		if(TextUtil.isNotEmpty(jobno)){
			map.put("jobno", jobno);
		}
		if(TextUtil.isNotEmpty(projectitemid)&&!projectitemid.equals("0")){
			map.put("projectitemid", projectitemid);
		}
		if(TextUtil.isNotEmpty(projectitemchildid)&&!projectitemchildid.equals("0")){
			map.put("projectitemchildid", projectitemchildid);
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo user = ((UserInfo)currentUser.getSession().getAttribute("userInfo"));
		map.put("companyid",user.getCompanyid());
		List<PlanMain> list = jobberService.searchuser(map);
		result.setRows(list);
		result.setTotal(jobberService.countuser(map));
		return result;
	}
	@RequestMapping(value = "/dailyfinace.do",method = RequestMethod.GET)
	@ResponseBody
	public JSONObject dailyfinace(@RequestParam("projectitemid") int projectitemid,
			@RequestParam("fromdate") String fromdate,@RequestParam("todate") String todate){
		JSONObject result = new JSONObject();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("projectitemid",projectitemid);
		List<Map<String,Object>> list = new ArrayList<>();
		Calendar calendar = Calendar.getInstance();
		Date to = new Date();
		try{
			to = sdf.parse(todate);
			calendar.setTime(sdf.parse(fromdate));
			while(calendar.getTimeInMillis()<=to.getTime()){
				String date = sdf.format(calendar.getTime());
				Subject currentUser = SecurityUtils.getSubject();
				int companyid = ((UserInfo)currentUser.getSession().getAttribute("userInfo")).getCompanyid();
				map.put("date",date);
				map.put("companyid",companyid);
				List<Map<String,Object>> list2 = budgetItemService.searchmoneyinitem(map);

				List<Map<String, Object>> countList = new ArrayList<Map<String, Object>>();  
		        for (int i = 0; i < list2.size(); i++) {  
		            String gsId = list2.get(i).get("itemid").toString();
		            int flag = 0;// 0为新增数据，1为增加count  
		            for (int j = 0; j < countList.size(); j++) {  
		                String gsId_ = countList.get(j).get("itemid").toString();
		  
		                if (gsId.equals(gsId_)) {  
		                    Double sum = Double.parseDouble(list2.get(i).get("money").toString()) 
		                    		+ Double.parseDouble(countList.get(j).get("money").toString());  
		                    countList.get(j).put("money", sum);  
		                    flag = 1;  
		                    continue;  
		                }  
		            }  
		            if (flag == 0) {  
		                countList.add(list2.get(i));  
		            }  
		        } 
				
				calendar.add(Calendar.DAY_OF_YEAR,1);
				Map<String,Object> map3 = new HashMap<String,Object>();
				map3.put("list",countList);
				map3.put("date",date);
				list.add(map3);
			}
		}
		catch(ParseException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		result.put("info",list);
		
		return result;
	}
	
	@RequestMapping(value = "/loadbudgetmain.do",method = RequestMethod.POST)
	@ResponseBody
	public BudgetMain loadbudgetmain(@RequestParam(value = "projectitemid", required = false) Integer projectitemid){
		BudgetMain budgetMain = budgetMainService.searchbyprojectitemid(projectitemid);
		return budgetMain;
	}
}
